<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {

//===== 首页
//===== 一级导航页面
//===== 二级导航页面
//===== 权限管理页面
//===== 用户管理页面
//===== 密码设置
//===== 服务管理


    private $data_Nav1 = '';        //缓存一二级导航数据
    private $data_AuthGroup = '';   //缓存权限分组数据
    private $UserInfo = '';
    public function _initialize(){
        if(!session('UserInfo') || !session('uid')){
            header('location:'.U('Login/login'));exit;
        }else{
            $this->UserInfo = session('UserInfo');
        }
        $auth = new \Think\Auth();
        $User_Info = session('UserInfo');
        if($User_Info['name']!='admin'){
            if(!$auth->check(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME,session('uid'))){
                 echo '没有权限';exit;
            }
        }

        //缓存一级导航信息
        if($this->data_Nav1 == ''){
            //获取一级导航列表
            $class_Nav1 = new ModelNav1Controller();
            $this->data_Nav1 = $class_Nav1->Nav1_Get_List();

            //获取二级导航列表
            $class_Nav2 = new ModelNav2Controller();
            $data_Nav2 = $class_Nav2->Nav2_Get_All();
            foreach($this->data_Nav1['list'] as $k=>$v){
                $Nav1_Id = $v['nav1_id'];

                $arr = Array();             
                foreach($data_Nav2 as $v2){
                    $Nav2_Parent_Id =  $v2['nav2_parent'];
                    if($Nav1_Id == $Nav2_Parent_Id){
                        array_push($arr,$v2);
                    }
                }
                foreach($arr as $k3=>$v3){
                    $tmp = '';
                    foreach($arr as $k4=>$v4){
                        if($k4 >= count($arr)-1){
                            break;
                        }
                        if($v4['nav2_order'] > $arr[$k4+1]['nav2_order']){
                            $tmp = $arr[$k4];
                            $arr[$k4] = $arr[$k4+1];
                            $arr[$k4+1] = $tmp;
                        }
                    }
                }
                $this->data_Nav1['list'][$k]['nav2'] = $arr;
            }
        }

        //获取权限分组信息
        if($this->data_AuthGroup == ''){
            $m = M('Think_auth_group');
            $m_Nav2 = M('Admin_nav2');
            $data_AuthGroup = $m->select();
            foreach($data_AuthGroup as $k=>$v){
                foreach($data_AuthGroup as $k2=>$v2){
                    $tmp_data_AuthGroup='';
                    if($data_AuthGroup[$k2]['orders'] > $data_AuthGroup[$k2+1]['orders']){
                        if($k2 < count($data_AuthGroup)-1){
                            $tmp_data_AuthGroup = $data_AuthGroup[$k2];
                            $data_AuthGroup[$k2] = $data_AuthGroup[$k2+1];
                            $data_AuthGroup[$k2+1] = $tmp_data_AuthGroup;
                        }
                    }
                }
            }
            foreach($data_AuthGroup as $k=>$v){
                $res_Nav2 = $m_Nav2->select($v['rules']);
                $arr = array();
                foreach($res_Nav2 as $v2){
                    array_push($arr,$v2['nav2_name']);
                }
                $data_AuthGroup[$k]['nav2'] = $arr;
            }
            $this->data_AuthGroup = $data_AuthGroup;
        }
    }

//===== token 设置
    public function token(){
        if(I('get.token')){
            $name = I('get.token');
        }
        if(I('post.token')){
            $name = I('post.token');
        }
        if(!$name){
            exit;
        }
        echo  Token_Set($name);
    }


//===== 首页
    public function index(){
        $url = 'http://'.$_SERVER['HTTP_HOST'];
        $this->assign('url',$url);

        $user_name = session('UserInfo')['name'];
        $this->assign('user_name',$user_name);
        //一级导航获取
        $this->assign('data_Nav1',$this->data_Nav1);
    	$this->display();
    }


//===== 一级导航页面
    public function admin_nav1(){
        $this->assign('data_Nav1',$this->data_Nav1);
        $this->assign('data_Nav1_Count',count($this->data_Nav1['list']));
        $this->display();
    }


//===== 二级导航页面
    public function admin_nav2(){
        $this->assign('data_Nav1',$this->data_Nav1);
        $this->display();
    }

        //二级导航列表
        public function admin_nav2_list(){
            $nav2_parent = I('get.nav2_parent');
            $where['nav2_parent']=$nav2_parent;
            $cls_Nav2 = new ModelNav2Controller();
            $data_nav2 = $cls_Nav2->Nav2_Get($where);
          // var_dump($data_nav2);
            $this->assign('data_nav2',$data_nav2);
            $this->assign('data_nav2_count',count($data_nav2['list']));
            $this->display();
        }



//===== 权限管理页面
    public function admin_auth_group(){
        $this->assign('data_Nav1',$this->data_Nav1);    //一二级导航列表

        $this->assign('data_AuthGroup',$this->data_AuthGroup);
        $this->display();
    }


        public function admin_auth_group_add(){
            $AuthTitle = I('post.AuthTitle');
            $AuthOrder = I('post.AuthOrder');
            $m = M('Think_auth_group');
            $res = $m->where(array('title'=>$AuthTitle))->find();
            if($res){
                echo "<script>alert('该分组名以存在！');history.go(-1);</script>";exit;
            }
            if(!$AuthTitle || !$AuthOrder){
                echo "<script>alert('分组名和序号不得为空！');history.go(-1);</script>";exit;
            }
            $p = '/^[1-9][0-9]{0,1}$/';
            if(!preg_match($p,$AuthOrder)){
                echo "<script>alert('序号必须是一到两位的整数！');history.go(-1);</script>";exit;
            }
            if(I('post.Rules')){
                $data['rules'] = implode(',',I('post.Rules'));
            }
            $data['title'] = $AuthTitle;
            $data['orders'] = $AuthOrder;
            if($m->create($data)){
                $st = $m->add();
                if($st > 0){
                    echo "<script>alert('添加成功！');location.href='admin_auth_group';</script>";exit;
                }
            }
            echo "<script>alert('添加失败！');history.go(-1);</script>";exit;
        }


        public function admin_auth_group_edit(){
            if(!I('get.id')){
                echo "<script>alert('id is miss');history.go(-1);</script>";exit;
            }
            $data_GroupInfo = I('get.');
            $data_GroupInfo['rules'] = explode(',',$data_GroupInfo['rules']);
            $data_Nav1 = $this->data_Nav1;
            foreach($data_Nav1['list'] as $k=>$v){
                foreach($v['nav2'] as $k2=>$v2){
                   if(in_array($v2['nav2_id'],$data_GroupInfo['rules'])){
                        $data_Nav1['list'][$k]['nav2'][$k2]['checked'] = 1;
                   } 
                }
            }
            $this->assign('data_Nav1',$data_Nav1);    //一二级导航列表
            $this->assign('info',$data_GroupInfo);  //当前选择的分组信息
            $this->display();
        }



        public function admin_auth_group_edit_sub(){
            $AuthId = I('post.AuthId');
            $AuthTitle = I('post.AuthTitle');
            $AuthOrder = I('post.AuthOrder');
            $m = M('Think_auth_group');
            $data_Res1 = $m->where(array('id'=>array('neq',$AuthId),'title'=>$AuthTitle))->find();
            if($data_Res1){
                echo "<script>alert('该分组名以存在！');history.go(-1);</script>";exit;
            }
            if(!$AuthTitle || !$AuthOrder || !$AuthId){
                echo "<script>alert('分组名和序号不得为空或id丢失！');history.go(-1);</script>";exit;
            }
            $p = '/^[1-9][0-9]{0,1}$/';
            if(!preg_match($p,$AuthOrder)){
                echo "<script>alert('序号必须是一到两位的整数！');history.go(-1);</script>";exit;
            }
            if(I('post.Rules')){
                $data['rules'] = implode(',',I('post.Rules'));
            }else{
                $data['rules'] = '';
            }
            $data['title'] = $AuthTitle;
            $data['orders'] = $AuthOrder;
            $data['id'] = $AuthId;
            if($m->create($data)){
                $st = $m->save();
                if($st > 0){
                    echo "<script>alert('修改成功！');location.href='admin_auth_group';</script>";exit;
                }
            }
            echo "<script>alert('修改失败！');history.go(-1);</script>";exit;     
        }



        public function admin_auth_group_del(){
            $gid =  I('get.gid');
            $m = M('Think_auth_group');
            echo $m->delete($gid);
        }



//===== 用户管理页面
    public function admin_auth_user(){
        $m = M('Think_auth_user');
        $data_ThinkUser = Get_Fpage($m,$fields='',$where='',$order = 'id desc',$pcount = 10);
        $m_GroupAccess = M('Think_auth_group_access');
        foreach($data_ThinkUser['list'] as $k=>$v){
            $data_GroupAccess = $m_GroupAccess->where(array('uid'=>$v['id']))->find();
            $data_ThinkUser['list'][$k]['group_id'] = $data_GroupAccess["group_id"];
            if($data_GroupAccess["group_id"]){
                foreach($this->data_AuthGroup as $v){
                    if($data_GroupAccess["group_id"] == $v['id']){
                        $data_ThinkUser['list'][$k]['group_name'] = $v['title'];
                        $data_ThinkUser['list'][$k]['nav2'] = implode(',',$v['nav2']);
                    }
                }
            }
        }
        $this->assign('data_ThinkUser',$data_ThinkUser);
        $this->display();
    }


        public function admin_auth_user_add(){
            $User_Name = I('post.User_Name');
            $User_Pwd = I('post.User_Pwd');
            if(!$User_Name || !$User_Pwd){
                exit("e1");  //数据不得为空
            }
            $m = M('Think_auth_user');
            $res_ThinkUser = $m->where(array('name'=>$User_Name))->find();
            if($res_ThinkUser){
                exit('e2'); //用户名以存在
            }
            $data['name'] = $User_Name;
            $data['pwd'] = Encrypt($User_Pwd);
            $data['ctime'] = time();
            if($m->create($data)){
                if($m->add()){
                    exit('ok');     //添加用户成功
                }
            }
            exit('e3'); //添加用户失败
        }


        public function admin_auth_user_del(){
            $uid = I('post.uid');
            if(!$uid){
                exit('e1');
            }
            $m = M('Think_auth_user');
            $st = $m->delete($uid);
            if($st> 0){
                echo 'ok';
                $m_Access = M('Think_auth_group_access');
                $st= $m_Access->where(array('uid'=>$uid))->delete();
            }else{
                echo 'e2';
            }
        }



        public function admin_auth_user_edit(){
            $User_Info = I('get.');
          //  dump(I('get.'));
            $res_Group = $this->data_AuthGroup;
            foreach($res_Group as $k=>$v){
                if($v['id'] == $User_Info['gid']){
                    $res_Group[$k]['checked'] = 1;
                }
                $res_Group[$k]['nav2'] = implode(',',$res_Group[$k]['nav2'] );
            }
            $this->assign('res_Group',$res_Group);
            $this->assign('User_Info',$User_Info);
            $this->display();
        }

        public function admin_auth_user_deit_sub(){
            $uid = I('post.uid');
            $gid = I('post.group');
            if(!$uid || !$gid){
                echo "<script>alert('缺少参数！');history.go(-1);</script>";exit;
            }
            if($gid=='null'){
                $gid = 0;
            }

            $m = M('Think_auth_group_access');
            $res = $m->where(array('uid'=>$uid))->find();
            if($res){
                $st = $m->where(array('uid'=>$uid))->data(array('group_id'=>$gid))->save();
            }else{
                $st = $m->data(array('uid'=>$uid,'group_id'=>$gid))->add();
            }
            if($st){
                echo "<script>alert('设置成功！');location='admin_auth_user';</script>";exit;
            }else{
                echo "<script>alert('设置失败！');history.go(-1);</script>";exit;
            }
        }


//===== 密码设置
    public function admin_user_setting(){
        $this->display();
    }

        public function admin_user_setting_sub(){
            $Old_Pwd = I('post.Old_Pwd');
            $New_Pwd = I('post.New_Pwd');
            $New_Pwd2 = I('post.New_Pwd2');
            if(!$Old_Pwd || !$New_Pwd || !$New_Pwd2){
                echo "<script>alert('不得为空！');history.go(-1);</script>";exit;
            }
            if($New_Pwd2 != $New_Pwd){
                echo "<script>alert('新密码不一致！');history.go(-1);</script>";exit;
            }
            $m = M('Think_auth_user');
            $res_User = $m->where(array('id'=>$this->UserInfo['id']))->find();
            if(Encrypt($Old_Pwd) != $res_User['pwd']){
                echo "<script>alert('原密码不正确！');history.go(-1);</script>";exit;
            }
            if(Encrypt($New_Pwd) == $res_User['pwd']){
                echo "<script>alert('新旧密码相同！');history.go(-1);</script>";exit;
            }
            $st = $m->where(array('id'=>$this->UserInfo['id']))->data(array('pwd'=>Encrypt($New_Pwd)))->save();
            if($st){
                echo "<script>alert('密码修改成功！');history.go(-1);</script>";exit;
            }else{
                echo "<script>alert('密码修改失败！');history.go(-1);</script>";exit;
            }
        }



//===== 服务管理
    public function service_type(){
        $m = M('Service_type');
        $res = Get_Fpage($m,$fields='',$where='',$order = 'type_order',$pcount = 10);
        $this->assign('res',$res);
        $this->assign('res_list_count',count($res['list']));
        $this->display();        
    }

    public function service_type_sub(){
        $name = I('post.name');
        $order = I('post.order');
        if(!$name || !$order){
            echo "<script>alert('不得为空！');history.go(-1)</script>";exit;
        }
        if(!preg_match('/^[1-9][0-9]{0,2}$/',$order)){
            echo "<script>alert('序号只能为三位以下正整数！');history.go(-1)</script>";exit;
        }
        $m = M('Service_type');
        $res = $m->where(array('type_name'=>$name))->find();
        if($res){
            echo "<script>alert('服务名以存在！');history.go(-1)</script>";exit;
        }
        $st = $m->data(array('type_name'=>$name,'type_order'=>$order))->add();
        if($st > 0){
            echo "<script>alert('添加成功！');location='".U('service_type')."'</script>";exit;
        }else{
            echo "<script>alert('添加失败');location='".U('service_type')."'</script>";exit;
        }
    }

    public function service_del(){
        $token = I('post.token');
        $name = I('post.token_name');
        if(!Token_V($name,$token)){
            exit;
        }
        $Sid = I('post.sid');
        $m = M('Service_type');
        $st = $m->delete($Sid);
        if($st){
            echo 'ok';
        }
    }










  
}